package cc.sheji.controller;

import cc.sheji.Service.InformationService;
import cc.sheji.domain.Information;
import cc.sheji.domain.InformationQueryCriteria;
import cc.sheji.domain.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController

@RequestMapping(value = "/api/v1/information")
public class InformationController {

    @Autowired
    InformationService informationService;

    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(informationService.get(id));
    }

    @PostMapping()
    public JsonResult create(@RequestBody @Validated Information entity) {
        entity.setId(null);
        return JsonResult.Success(informationService.create(entity));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @RequestBody Information entity) {
        entity.setId(id);
        return JsonResult.Success(informationService.update(entity));
    }

    @GetMapping()
    public JsonResult get(InformationQueryCriteria criteria , Pageable pageable) throws InterruptedException {
        return JsonResult.Success(informationService.list(criteria,pageable));
    }

}
